package com.yukh.framework.mybatis.generator;

import java.io.Reader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.junit.Test;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.api.VerboseProgressCallback;
import org.mybatis.generator.config.CommentGeneratorConfiguration;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.JDBCConnectionConfiguration;
import org.mybatis.generator.config.ModelType;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

public class GeneratorMyBatis {
	
	@Test
	public void generator4Xml(){
		try {
			// java -jar mybatis-generator-core-1.3.0.jar -configfile ../src/main/resource/config.xml -overwrite  
			Reader reader = Resources.getResourceAsReader("com/yukh/framework/mybatis/generator/generator-config.xml");
			
			List<String> warnings = new ArrayList<String>();
			ConfigurationParser cp = new ConfigurationParser(warnings);
			Configuration config = cp.parseConfiguration(reader);
			boolean overwrite = true;
			DefaultShellCallback callback = new DefaultShellCallback(overwrite);
			MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
			myBatisGenerator.generate(new VerboseProgressCallback(), new HashSet<String>(), new HashSet<String>());
//			myBatisGenerator.generate(null, new HashSet<String>(), new HashSet<String>());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public void generatorNoXml(){
		Configuration configuration = new Configuration();
		
		ModelType mt = ModelType.HIERARCHICAL;							//	defaultModelType
		Context context = new Context(mt);								//  <Context></context>
		context.setId("DB2Tables");
		context.setTargetRuntime("MyBatis3");
		configuration.addContext(context);
		
		// <commentGenerator></commentGenerator>
		CommentGeneratorConfiguration commentGeneratorConfiguration = new CommentGeneratorConfiguration();
		commentGeneratorConfiguration.addProperty("suppressDate", "true");
		context.setCommentGeneratorConfiguration(commentGeneratorConfiguration);
		// <jdbcConnection></jdbcConnection>
		JDBCConnectionConfiguration jdbcConnectionConfiguration = new JDBCConnectionConfiguration();
		jdbcConnectionConfiguration.setDriverClass("com.mysql.jdbc.Driver");
		jdbcConnectionConfiguration.setConnectionURL("jdbc:mysql://localhost:3309/mybatis");
		jdbcConnectionConfiguration.setUserId("root");
		jdbcConnectionConfiguration.setPassword("111111");
		context.setJdbcConnectionConfiguration(jdbcConnectionConfiguration);
		
		// 1. init: MyBatisGenerator.generate()    GeneratedJavaFile��GeneratedXmlFile
		// 2. PluginAggregator 
		// 
		
		// config.addClasspathEntry("");
		
	}
}
